package cn.wolfcode.wolf2w.util;

import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

public class DBHelper {
    //分页抽取工具类
    public static<T> Page<T>query(Query query, Class<T> clz, MongoTemplate template, Pageable pageable){
        //获取总条数
        long total = template.count(query, clz);
        //返回空分页对象
        if (total == 0) {
            return Page.empty();
        }
        //存入查询条件中
        query.with(pageable);
        //查询内容
        List<T> list = template.find(query, clz);
        //返回PageImpl实现类,Page是接口不能返回
        //1:内容,2:当前页数据,3:总条数
        return new PageImpl<>(list, pageable, total);
    }
}
